package org.overtheair.say.logic;

import java.util.ArrayList;
import java.util.List;

import org.overtheair.say.common.GlobalHelper;
import org.overtheair.say.domain.DataMovie;
import org.overtheair.say.domain.QueryType;


public class DataProviderMovie
	extends DataProviderAncestor
{
	
	//*************************************************************************
	// public methods
	//*************************************************************************

	
	public static List<DataMovie> getMovies(String location)
	{
		return getMovies(QueryType.FixedLocation, location, 0, 0);
	}

	/**
	 * Return a set of Flickr photos near coordinates
	 * @param latitude
	 * @param longitude
	 * 
	 * @return
	 */
	public static List<DataMovie> getMovies(double latitude, double longitude)
	{
		return getMovies(QueryType.CurrentLocation, "", latitude, longitude);
	}
	



	
	//*************************************************************************
	// private methods
	//*************************************************************************

	/**
	 * @param queryType 1: search by coordinates, 2 search by location
	 */
	
	private static List<DataMovie> getMovies(
			QueryType queryType,
			String location,
			double latitude,
			double longitude)
	{
		List<DataMovie> listResult;
		String movieUrl;
		
		listResult = new ArrayList<DataMovie>();
		
	    //Recorded accuracy level of the location information.
		// World level is 1, Country is ~3, Region ~6, City ~11, Street ~16. Current range is 1-16. The default is 16.
		
		//set webservice query
		if (queryType == QueryType.CurrentLocation)
			movieUrl = getUpcomingUrlForCoordinates(latitude, longitude, 0);
		else
			movieUrl = getUpcomingUrlForLocation(location, 0);
			
		//try to get info from webservice
		WebserviceRequestClient wsClient = new WebserviceRequestClient();
		String webResult = wsClient.RequestGET(movieUrl);
		
		if (GlobalHelper.IsNullOrEmpty(webResult))
			return listResult;
		
		//split the string line by line
		if (!GlobalHelper.IsNullOrEmpty(webResult))
		{
			String[] dataLines = webResult.split("\n");

			//examine the reply
				for (int i = 0; i < dataLines.length; i ++)
			{
				listResult.add(new DataMovie(dataLines[i]));
			}

		}

		return listResult;
	}

	/**
	 * Get the url for flickr webservice to get photos by latitude and longitude
	 */
	private static String getUpcomingUrlForCoordinates(double latitude, double longitude, int accuracy)
	{

		String newUrl =
			"http://she.xiaprojects.com/sayCinemaThumb2.php?uri=London";
		return newUrl;
	}


	private static String getUpcomingUrlForLocation(String location, int accuracy)
	{
		String newUrl =
			"http://she.xiaprojects.com/sayCinemaThumb2.php?uri=London";
		return newUrl;
	}
}
